package org.example.service;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.ByteArrayOutputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.example.pojo.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

@Service
public class ExportService {

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    public byte[] generateWord() throws Exception {
        // 1. 准备数据（模拟多行数据）
        List<Item> items = new ArrayList<>();
        items.add(new Item("05418996", "输电运检中心输电带电一班工器具室", "国网商丘供电公司-输电运检中心", "安全带-全身式,z-坠落悬挂"));
        items.add(new Item("051297", "汤阴变安全工器具室", "国网安阳供电公司-变电运维中心", "安全带-半身式,z-坠落悬挂"));
        items.add(new Item("04846653", "输电运检中心输电检修二班工器具室", "国网商丘供电公司-输电运检中心", "安全带-全身式,z-坠落悬挂"));
        items.add(new Item("051303", "汤阴变安全工器具室", "国网安阳供电公司-变电运维中心", "安全带-半身式,z-坠落悬挂"));
        items.add(new Item("04846660", "输电运检中心输电检修二班工器具室", "国网商丘供电公司-输电运检中心", "安全带-全身式,z-坠落悬挂"));

        // 2. 为每个 Item 生成二维码（Base64 字节流）
        for (Item item : items) {
            item.setQrCodeBytes(generateQRCode(item.getId()));
        }

        // 3. Freemarker 渲染模板
        Map<String, Object> model = new HashMap<>();
        model.put("items", items);
        Configuration config = freeMarkerConfigurer.getConfiguration();
        Template template = config.getTemplate("word.ftl");
        StringWriter writer = new StringWriter();
        template.process(model, writer);

        // 4. POI 生成 Word 文档
        XWPFDocument doc = new XWPFDocument();
        XWPFParagraph paragraph = doc.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText(writer.toString()); // 插入模板渲染后的文本
        run.setFontSize(12);
        run.setFontFamily("宋体");

        // 5. 转换为二进制流并返回
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        doc.write(outputStream);
        return outputStream.toByteArray();
    }

    /**
     * 生成二维码并转为 Base64 字节流
     * @param content 二维码内容（如 ID）
     * @return 二维码字节流
     */
    private byte[] generateQRCode(String content) throws Exception {
        // 生成二维码
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, 200, 200); // 宽高：200px
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    }
}
